package in.muton.panels.paginator.badgehelpers.user;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.auth.roles.UnivRoleCheck;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.wicket.model.IModel;

import com.edushare.common.dao.GroupsMembersDAO;
import com.edushare.common.vo.GroupMembers;
import com.edushare.common.vo.Groups;
import com.edushare.common.vo.Users;

public class GroupBadgeRoles extends BadgeRoles {

	private static final long serialVersionUID = 2948077789320992237L;
	private IModel<Groups> groupModel;
	private static final HashMap<String, Integer> roleMap = new HashMap<String, Integer>();
	
	static{
		roleMap.put("Admin", GroupMembers.GROUP_ADMIN);
		roleMap.put("Member", GroupMembers.GROUP_MEMBER);
	}
	
	public GroupBadgeRoles(IModel<Groups> gm) {
		this.groupModel = gm;
	}
	
	@Override
	public String changeRole(Users user, String role) {
		if(isLastAdmin(user))
			return "Cannot change role for user "+user.getCompleteUserName()+ 
			" as this user is the last administrator for this group.";
		
		for(GroupMembers gm: user.getGroupMemberships()){
			if(gm.getGroup().getObjectId().equals(groupModel.getObject().getObjectId())){
				gm.setRole(roleMap.get(role));
				new GroupsMembersDAO(EduShareSession.get().getSessionORMInterface()).update(gm);
				break;
			}
		}
		return "";
	}

	@Override
	public List<String> getOtherRoles(Users user) {
		List<String> othRoles = new ArrayList<String>();
		if(user.isGroupAdmin(groupModel.getObject())){
			othRoles.add("Member");
		}else{
			othRoles.add("Admin");
		}
		return othRoles;
	}

	@Override
	public String getRole(Users user) {
		if(user.isGroupAdmin(groupModel.getObject()))
			return "Admin";
		else 
			return "Member";
	}

	@Override
	public boolean isAdmin() {
		return EduShareSession.get().getLoggedInUser().isGroupAdmin(groupModel.getObject()) || UnivRoleCheck.isAdmin(groupModel.getObject().getUniversity(), EduShareSession.get().getLoggedInUser());
	}

	@Override
	public boolean isInvited(Users user) {
		boolean isInvited = false;
		
		for(GroupMembers gm : user.getGroupMemberships()){
			if(gm.getGroup().getObjectId().equals(groupModel.getObject().getObjectId())){
				if(gm.getStatus() == GroupMembers.STATUS_INVITED)
					isInvited = true;
				break;
			}
		}
		return isInvited;
	}

	@Override
	public String removeUser(Users user) {
		if(isLastAdmin(user))
			return "Cannot remove user "+ user.getCompleteUserName()+
			" as this user is the last administrator for this group";
		for(GroupMembers gm: user.getGroupMemberships()){
			if(gm.getGroup().getObjectId().equals(groupModel.getObject().getObjectId())){
				new GroupsMembersDAO(EduShareSession.get().getSessionORMInterface()).delete(gm);
				break;
			}
		}
		return "";
	}

	@Override
	public boolean isLastAdmin(Users user) {
		if(
				roleMap.get(getRole(user))!=null &&
				roleMap.get(getRole(user)).equals(GroupMembers.GROUP_ADMIN) &&
				new GroupsMembersDAO(EduShareSession.get().getSessionORMInterface())
					.getMembersByType(groupModel.getObject(), GroupMembers.GROUP_ADMIN).size() <2
		)
		{
			return true;
		}
		return false;
	}

}
